package cn.iocoder.yudao.module.dish.controller.admin.dishCategory;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dish.controller.admin.dishCategory.vo.*;
import cn.iocoder.yudao.module.dish.dal.dataobject.dishCategory.DishCategoryDO;
import cn.iocoder.yudao.module.dish.service.dishCategory.DishCategoryService;

@Tag(name = "管理后台 - 餐品分类")
@RestController
@RequestMapping("/dish/category")
@Validated
public class DishCategoryController {

    @Resource
    private DishCategoryService categoryService;

    @PostMapping("/create")
    @Operation(summary = "创建餐品分类")
    @PreAuthorize("@ss.hasPermission('dish:category:create')")
    public CommonResult<Long> createCategory(@Valid @RequestBody DishCategorySaveReqVO createReqVO) {
        return success(categoryService.createCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新餐品分类")
    @PreAuthorize("@ss.hasPermission('dish:category:update')")
    public CommonResult<Boolean> updateCategory(@Valid @RequestBody DishCategorySaveReqVO updateReqVO) {
        categoryService.updateCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除餐品分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dish:category:delete')")
    public CommonResult<Boolean> deleteCategory(@RequestParam("id") Long id) {
        categoryService.deleteCategory(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得餐品分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dish:category:query')")
    public CommonResult<DishCategoryRespVO> getCategory(@RequestParam("id") Long id) {
        DishCategoryDO category = categoryService.getCategory(id);
        return success(BeanUtils.toBean(category, DishCategoryRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得餐品分类分页")
    @PreAuthorize("@ss.hasPermission('dish:category:query')")
    public CommonResult<PageResult<DishCategoryRespVO>> getCategoryPage(@Valid DishCategoryPageReqVO pageReqVO) {
        PageResult<DishCategoryDO> pageResult = categoryService.getCategoryPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DishCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出餐品分类 Excel")
    @PreAuthorize("@ss.hasPermission('dish:category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCategoryExcel(@Valid DishCategoryPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DishCategoryDO> list = categoryService.getCategoryPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "餐品分类.xls", "数据", DishCategoryRespVO.class,
                        BeanUtils.toBean(list, DishCategoryRespVO.class));
    }

}